

package com.xuelangyun.form.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.xuelangyun.form.common.exception.DyformException;
import com.xuelangyun.form.common.log.annotation.SysLog;
import com.xuelangyun.form.common.utils.R;
import com.xuelangyun.form.sys.entity.SysMenuEntity;
import com.xuelangyun.form.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuelangyun.form.common.security.AuthSecurityUtils;
import com.xuelangyun.form.sys.service.ISysMenuService;

/**
 * 系统菜单
 *
 * @author weiqing.hk
 * @date 2024年4月27日 下午9:58:15
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuNavController {
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private AuthSecurityUtils authUtils;

    /**
     * 选择菜单(添加、修改菜单)
     */
    @SuppressWarnings("rawtypes")
    @GetMapping("/select")
    public R<List> selectList() {
        //查询列表数据
        List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();

        //添加顶级菜单
        SysMenuEntity root = new SysMenuEntity();
        root.setMenuId("0");
        root.setName("一级菜单");
        root.setParentId("-1");
        root.setOpen(true);
        menuList.add(root);
        return R.ok(List.class).setData(menuList);
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{menuId}")
    public R<SysMenuEntity> menuInfo(@PathVariable("menuId") String menuId) {
        SysMenuEntity menu = sysMenuService.getById(menuId);
        return R.ok(SysMenuEntity.class).setData(menu);
    }

    /**
     * 导航菜单
     */
    @SuppressWarnings("rawtypes")
    @GetMapping("/nav")
    public R<Map> navMap() {

        SysUserEntity user = authUtils.getCurrentUser();

        List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(user.getUserId());
        Set<String> permissions = sysMenuService.queryAllPerms(user.getUserId());

        Map<String, Object> map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("permissions", permissions);

        return R.ok(Map.class).setData(map);
    }

    /**
     * 所有菜单列表
     */
    @SuppressWarnings("rawtypes")
    @GetMapping("/list")
    public R<List> menuList() {

        QueryWrapper<SysMenuEntity> menuQuery = new QueryWrapper<>();
        menuQuery.orderByAsc("order_num");

        List<SysMenuEntity> menuList = sysMenuService.list(menuQuery);

        Map<String, String> id_name = new HashMap<>();
        for (SysMenuEntity m : menuList) {
            id_name.put(m.getMenuId(), m.getName());
        }

        for (SysMenuEntity sysMenuEntity : menuList) {
            String pname = id_name.get(sysMenuEntity.getParentId());

            if (pname != null) {
                sysMenuEntity.setParentName(pname);
            }
        }
        return R.ok(List.class).setData(menuList);
    }

    /**
     * 保存
     */
    @SysLog("保存菜单")
    @PostMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public R<Object> saveMenu(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.save(menu);
        return R.ok();
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenuEntity menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new DyformException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new DyformException("上级菜单不能为空");
        }

        //菜单
		/*if(menu.getType() == MenuType.MENU ){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}

		//上级菜单类型
		MenuType parentType = MenuType.CATALOG ;
		if(!menu.getParentId().equals("0")){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}*/

        //目录、菜单
		/*if(menu.getType() == MenuType.CATALOG.getValue() ||
				menu.getType() == MenuType.MENU.getValue()){
			if(parentType != MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}

		//按钮
		if(menu.getType() == MenuType.BUTTON.getValue()){
			if(parentType != MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}*/
    }

    /**
     * 修改
     */
    @SysLog("修改菜单")
    @PostMapping("/update")
    @RequiresPermissions("sys:menu:update")
    public R<Object> updateMenu(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.updateById(menu);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除菜单")
    @PostMapping("/delete/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    public R<Object> deleteMenu(@PathVariable("menuId") String menuId) {
		/*if(menuId <= 31){
			return R.error("系统菜单，不能删除");
		}
        */
        //判断是否有子菜单或按钮
        List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
        if (menuList.size() > 0) {
            return R.error("请先删除子菜单或按钮");
        }
        sysMenuService.delete(menuId);
        return R.ok();
    }
}
